#!/bin/bash
set -e

if [ $# -eq 2 -a "x${2}" = "xsubbuild" ]; then
	echo "配置环境变量: [已配置]"
else
	echo "配置环境变量: [开始]"
	#>>-----------------------------------------------------------------------------------
	#切换目录
	cd ../
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#顶层项目目录
	export PROJECT_TOPDIR=$PWD
	#顶层当前配置文件
	export CURCFG_FILE=${PROJECT_TOPDIR}/.curconfig.sh
	#-------------------------------------------------------------------------------------

	#>>env--------------------------------------------------------------------------------
	#设置颜色函数
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.000.colorsetup.sh
	#导入公共变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.001.envsetup_common.sh
	#导入平台变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.002.envsetup_platform.sh
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#切回目录
	cd -> /dev/null
	#-------------------------------------------------------------------------------------
	echo "配置环境变量: [完毕]"
fi

#-----------------------------------------------------------------------
export MODPKG_ROOT=${MIDKIT_TOPDIR}/pkg
export MODSRC_ROOT=${MIDKIT_TOPDIR}/src

export MODNAME=busybox
export MODPKG_NAME=busybox-1.23.2.tar.gz
export MODSRC_NAME=busybox-1.23.2

export MODPKG_PATH=${MODPKG_ROOT}/${MODPKG_NAME}
export MODSRC_PATH=${MODSRC_ROOT}/${MODSRC_NAME}
export MODOUT_PATH=${MIDKIT_TOPDIR}/out/z.output.${MODNAME}

export MODCFG_DEF_PATH=${MODSRC_PATH}/configs/dugi_defconfig
#export MODCFG_PLT_INFO="-mfloat-abi=soft"
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
#导入公共函数
source 000.import.commonfunc.sh
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
function menuconfig_module()
{
	sinfo "图形配置busybox: [开始]"
	if [ ! -f $MODSRC_PATH/.config ]; then
		serro "没有找到文件: .config"
		exit 1
	fi
	(cd ${MODSRC_PATH} && make menuconfig)
    [ $? -ne 0 ] && serro "图形配置busybox： [失败]" && exit 1
	sdone "图形配置busybox: [完成]"
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
function defconfig_module()
{ 
	sinfo "默认配置busybox: [开始]"
	if [ -f $MODCFG_DEF_PATH ]; then
		sinfo "cp \t$MODCFG_DEF_PATH \n\t${MODSRC_PATH}/.config"
		cp $MODCFG_DEF_PATH ${MODSRC_PATH}/.config
		sdone "默认配置busybox: [成功]"
	else
		serro "默认配置busybox: [失败]"
		exit 1
	fi

	change_value_line $MODSRC_PATH/.config CONFIG_CROSS_COMPILER_PREFIX $GCC_TYPE- 1 0
	[ $? -ne 0 ] && serro "配置编译器变量\t: CONFIG_CROSS_COMPILER_PREFIX \t[失败]" && exit 1
	sdone "配置编译器变量\t: CONFIG_CROSS_COMPILER_PREFIX \t[成功]"

	change_value_line $MODSRC_PATH/.config CONFIG_PREFIX $MODOUT_PATH 1 0
	[ $? -ne 0 ] && serro "配置源码变量\t: CONFIG_PREFIX \t[失败]" && exit 1
	sdone "配置源码变量\t: CONFIG_PREFIX \t[成功]"

#	change_value_line $MODSRC_PATH/.config CONFIG_EXTRA_CFLAGS "$MODCFG_PLT_INFO" 1 0
#	[ $? -ne 0 ] && serro "配置编译标记\t: CONFIG_EXTRA_CFLAGS \t[失败]" && exit 1
#	sdone "配置编译标记\t: CONFIG_EXTRA_CFLAGS \t[成功]"
#	sinfo "默认配置busybox: [结束]"
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
function build_module()
{
	sinfo "编译开始: [开始]"
	if [ ! -d ${MODOUT_PATH} ]; then
		mkdir -p ${MODOUT_PATH}
	fi

	if [ ! -f ${MODSRC_PATH}/.config ]; then
		defconfig_module
	fi

	(cd ${MODSRC_PATH} && make )
    [ $? -ne 0 ] && serro "编译开始: [失败]" && exit 1
	sdone "编译模块: [完成]"

	sinfo "安装模块: [开始]"
	(cd ${MODSRC_PATH} && make install)
    [ $? -ne 0 ] && serro "安装模块: [失败]" && exit 1
	sdone "安装模块: [完成]"
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
function clean_module()
{
	sinfo "清除模块: [开始]"
	if [ -f ${MODSRC_PATH}/Makefile ]; then
		(cd ${MODSRC_PATH} && make clean)
		[ $? -ne 0 ] && serro "清除模块: [失败]"	&& exit 1
	fi
	sdone "清除模块: [完成]"
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
function mainproc() 
{
	if [ "x$1" = "xdefconfig" ]; then
		check_env
		check_source
		defconfig_module
	elif [ "x$1" = "xbuild" ]; then
		check_env
		check_source
		build_module
	elif [ "x$1" = "xmenuconfig" ]; then
		check_env
		check_source
		menuconfig_module
	elif [ "x$1" = "xclean" ]; then
		check_env
		clean_module
	elif [ "x$1" = "xpack" ]; then
		pack_module
	else
		sinfo "输入: $0 defconfig    ${MODNAME}默认配置"
		sinfo "输入: $0 build        ${MODNAME}源码编译"
		sinfo "输入: $0 menuconfig   ${MODNAME}图形配置"
		sinfo "输入: $0 clean        ${MODNAME}清除编译"
		sinfo "输入: $0 pack         ${MODNAME}源码打包"
	fi
}
mainproc $1
